﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BusinessLayer;
using Ocean.OceanFramework.MVVM;
using Ocean.OceanFramework.CommonDialog;
using BusinessEntityObjects;
using System.Collections.ObjectModel;
using Ocean.OceanFramework.WPF.Utility;
using System.Windows.Input; 

namespace WPFLOBMVVM.ViewModel
{
    public class EditContactsAjaxComboBoxSelectionViewModel : ContainerFormViewModelBase
    {

        #region " Declarations "

        private ICommand _cmdDeleteCommand;
        private ICommand _cmdNewCommand;
        private ICommand _cmdSaveCommand;
        private ContactViewModel _objContactViewModel;

        #endregion

        #region " Events "

        public event ClearAjaxComboBoxEventHandler ClearAjaxComboBox;
        public delegate void ClearAjaxComboBoxEventHandler(object sender, EventArgs e);

        #endregion

        #region " Properties "

        public ContactViewModel ContactViewModel
        {
            get { return _objContactViewModel; }

            private set
            {
                _objContactViewModel = value;
                OnPropertyChanged("ContactViewModel");
            }
        }

        public override bool IsDirty
        {
            get { return _objContactViewModel != null && _objContactViewModel.Contact != null && _objContactViewModel.Contact.IsDirty; }
        }

        #endregion

        #region " Command Properties "

        public ICommand DeleteCommand
        {
            get
            {

                if (_cmdDeleteCommand == null)
                {
                    _cmdDeleteCommand = new RelayCommand( param => DeleteExecute(), this.CanDeleteExecute);
                }

                return _cmdDeleteCommand;
            }
        }

        public ICommand NewCommand
        {
            get
            {

                if (_cmdNewCommand == null)
                {
                    _cmdNewCommand = new RelayCommand(param => this.NewExecute());
                }

                return _cmdNewCommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {

                if (_cmdSaveCommand == null)
                {
                    _cmdSaveCommand = new RelayCommand( param => this.SaveExecute());
                }

                return _cmdSaveCommand;
            }
        }

        #endregion

        #region " Constructor "

        public EditContactsAjaxComboBoxSelectionViewModel()
        {
            base.ViewModelFriendlyName = "WPF Disciples";
            NewExecute();
        }

        #endregion

        #region " Command Methods "

        public int DeleteExecute()
        {
            Helpers.UpdateFocusedField();

            string strSaveActiveRuleSet = this.ContactViewModel.Contact.ActiveRuleSet;

            try
            {
                this.ContactViewModel.Contact.ActiveRuleSet = "Delete";

                if (this.ContactViewModel.Contact.IsValid())
                {

                    if (ViewModelUIService.YesNoConfirmDelete(0) == CustomDialogResult.Yes)
                    {

                        if (ContactBLL.CreateInstance().Delete(this.ContactViewModel.Contact) == 1)
                        {
                            NewExecute();
                            ContactViewModel.WatermarkMessage = "Record deleted";
                            OnClearAjaxComboBox();
                            return 1;
                        }

                        else
                        {
                            ViewModelUIService.MessageDialog("Delete Failure", "Delete Failed", "Could not delete contact", 0);
                        }
                    }

                }

                else
                {
                    this.ContactViewModel.Contact.ActiveRuleSet = strSaveActiveRuleSet;
                    ContactViewModel.ErrorHeaderText = "Record invalid for delete";
                }
            }

            catch (Exception ex)
            {
                ViewModelUIService.ExceptionDialog(ex, 0);
            }

            return 0;
        }

        public int NewExecute()
        {
            Helpers.UpdateFocusedField();

            try
            {

                if (_objContactViewModel == null || VerifyDataBeforeClosing())
                {
                    LoadViewModel(BusinessLayer.ContactBLL.CreateInstance().Create());
                    ContactViewModel.ErrorHeaderText = "Required Items";
                    return 1;
                }

                else
                {
                    return 0;
                }
            }

            catch (Exception ex)
            {
                ViewModelUIService.ExceptionDialog(ex, 0);
            }
            //JAIMER 
            return 0; 

        }

        public override int SaveExecute()
        {
            Helpers.UpdateFocusedField();

            if (this.ContactViewModel.Contact.IsDirty)
            {

                if (this.ContactViewModel.Contact.Id == 0)
                {
                    return Insert();
                }

                else
                {
                    return Update();
                }
            }


            return -1;
        }

        private bool CanDeleteExecute(object param)
        {

            if (_objContactViewModel == null || _objContactViewModel.Contact == null)
            {
                return false;
            }

            return _objContactViewModel.Contact.Id != 0;
        }

        #endregion

        #region " Methods "

        public int Insert()
        {

            try
            {

                if (this.ContactViewModel.Contact.IsValid())
                {

                    int intNewId = 0;

                    if (ContactBLL.CreateInstance().Insert(this.ContactViewModel.Contact, ref intNewId) == 1)
                    {
                        //this simulates getting the object fresh from the database after the insert, timestamp and other fields we may not have 
                        LoadViewModel(ContactBLL.CreateInstance().Select(intNewId));
                        ContactViewModel.WatermarkMessage = "Record inserted";
                        return 1;
                    }
                }


                ContactViewModel.ErrorHeaderText = "Save Errors";
            }

            catch (Exception ex)
            {
                ViewModelUIService.ExceptionDialog(ex, 0);
            }

            return 0;
        }

        public List<ContactSearchResult> Search(string strSearchString)
        {
            return ContactBLL.CreateInstance().SearchByLastName(strSearchString);
        }

        public void Select(int intId)
        {

            try
            {

                Contact obj = ContactBLL.CreateInstance().Select(intId);

                if (obj != null)
                {
                    LoadViewModel(obj);
                    ContactViewModel.ErrorHeaderText = "Required Items";
                }

                else
                {
                    ViewModelUIService.MessageDialog("Selection Failed", "Selection Failed", string.Format("Could not select contact, Id:{0}", intId), 0);
                }
            }

            catch (Exception ex)
            {
                ViewModelUIService.ExceptionDialog(ex, 0);
            }

        }

        public int Update()
        {

            try
            {

                int intSaveId = this.ContactViewModel.Contact.Id;

                if (this.ContactViewModel.Contact.IsValid())
                {

                    if (ContactBLL.CreateInstance().Update(this.ContactViewModel.Contact) == 1)
                    {
                        LoadViewModel(ContactBLL.CreateInstance().Select(intSaveId));
                        ContactViewModel.WatermarkMessage = "Record saved";
                        return 1;
                    }
                }


                ContactViewModel.ErrorHeaderText = "Save Errors";
            }

            catch (Exception ex)
            {
                ViewModelUIService.ExceptionDialog(ex, 0);
            }

            return 0;
        }

        private void LoadViewModel(Contact obj)
        {

            if (_objContactViewModel == null)
            {
                this.ContactViewModel = new ContactViewModel(obj);
            }

            else
            {
                _objContactViewModel.Contact = obj;
            }

        }

        private void OnClearAjaxComboBox()
        {
            //TODO : JAIMER 
            if (this.ClearAjaxComboBox != null)
            {
                if (ClearAjaxComboBox != null)
                {
                    ClearAjaxComboBox(this, EventArgs.Empty);
                }
            }

        }
    }

        #endregion 
}
